package com.zh.common.view.text

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.zh.common.R
import com.zh.common.ktx.color
import com.zh.common.ktx.dp2px
import com.zh.common.ktx.sp2px

/**
 * @auth zh
 * @time 2022/9/23 - 15:24
 * @desc 自定义文本，可设置圆角，颜色
 */
class CustomTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var mText: String? = "未知"
    private var mTextSize: Float = 12F.sp2px.toFloat()
    private var mTextColor: Int = R.color.color_white.color()
    private var mCorners: Float = 2F.dp2px.toFloat()
    private var mPaddingVertical: Float = 4F.dp2px.toFloat()
    private var mPaddingHorizontal: Float = 8F.dp2px.toFloat()
    private var mBackground: Int = R.color.colorPrimary.color()

    //边框背景
    private var mFrame: Boolean = false
    private var mFrameColor: Int = R.color.color_text_999.color()
    private var mFrameWidth: Float = 0.5F.dp2px.toFloat()

    private val paint by lazy {
        Paint().apply {
            isAntiAlias = true
            textSize = mTextSize
            style = Paint.Style.FILL_AND_STROKE
            textAlign = Paint.Align.CENTER
        }
    }

    private val rectF by lazy {
        RectF(
            0f + 0.5.dp2px,
            0f + 0.5.dp2px,
            width.toFloat() - 0.5.dp2px,
            height.toFloat() - 0.5.dp2px
        )
    }

    init {
        val typedArray =
            context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, defStyleAttr, 0)
        mText = if (typedArray.hasValue(R.styleable.CustomTextView_ctv_text))
            typedArray.getString(R.styleable.CustomTextView_ctv_text) else mText
        mTextSize = typedArray.getDimension(R.styleable.CustomTextView_ctv_textSize, mTextSize)
        mTextColor = typedArray.getColor(R.styleable.CustomTextView_ctv_textColor, mTextColor)
        mCorners = typedArray.getDimension(R.styleable.CustomTextView_ctv_corners, mCorners)
        mPaddingVertical = typedArray.getDimension(
            R.styleable.CustomTextView_ctv_paddingVertical, mPaddingVertical
        )
        mPaddingHorizontal = typedArray.getDimension(
            R.styleable.CustomTextView_ctv_paddingHorizontal, mPaddingHorizontal
        )
        mBackground = typedArray.getColor(R.styleable.CustomTextView_ctv_background, mBackground)
        mFrame = typedArray.getBoolean(R.styleable.CustomTextView_ctv_frame, mFrame)
        mFrameColor = typedArray.getColor(R.styleable.CustomTextView_ctv_frameColor, mFrameColor)
        mFrameWidth =
            typedArray.getDimension(R.styleable.CustomTextView_ctv_frameWidth, mFrameWidth)
        typedArray.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //文字高度
        val frameMetrics = paint.fontMetrics
        val textHeight = frameMetrics.descent - frameMetrics.ascent
        //文字宽度
        val width = paint.measureText(mText)
        setMeasuredDimension(
            width.toInt() + mPaddingHorizontal.toInt() * 2,
            textHeight.toInt() + mPaddingVertical.toInt() * 2
        )
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas ?: return

        if (mFrame) {//边框背景
            paint.color = mFrameColor
            paint.strokeWidth = mFrameWidth
            paint.style = Paint.Style.STROKE
        } else {//纯色背景
            paint.color = mBackground
        }
        canvas.drawRoundRect(rectF, mCorners, mCorners, paint)

        paint.strokeWidth = 0f
        paint.color = mTextColor
        paint.style = Paint.Style.FILL_AND_STROKE
        val frameMetrics = paint.fontMetrics
        val baseline =
            rectF.centerY() + (frameMetrics.bottom - frameMetrics.top) / 2 - frameMetrics.bottom
        mText?.let {
            canvas.drawText(it, rectF.centerX(), baseline, paint)
        }
    }

    fun setText(src: String): CustomTextView {
        mText = src
        return this
    }

    fun setTextColor(color: Int): CustomTextView {
        mTextColor = color
        return this
    }

    fun setTextSize(value: Float): CustomTextView {
        mTextSize = value
        return this
    }

    fun setBackground(color: Int): CustomTextView {
        mBackground = color
        return this
    }

    fun setPaddingVertical(value: Float): CustomTextView {
        mPaddingVertical = value
        return this
    }

    fun setPaddingHorizontal(value: Float): CustomTextView {
        mPaddingHorizontal = value
        return this
    }

    fun setFrame(value: Boolean): CustomTextView {
        mFrame = value
        return this
    }

    fun setFrameColor(value: Int): CustomTextView {
        mFrameColor = value
        return this
    }

    fun setFrameWidth(value: Float): CustomTextView {
        mFrameWidth = value
        return this
    }

    /**
     * 自定义最后比调的
     */
    fun create() {
        invalidate()
    }
}